package com.ai.mapper.security;

import com.ai.entity.security.SysPermission;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 权限表 Mapper 接口
 * </p>
 *
 * @author chen
 * @since 2023-04-04 19:46:06
 */
public interface SysPermissionMapper extends BaseMapper<SysPermission> {


    /**
     * 查询用户的权限
     *
     * @param userId
     * @return
     */
    @Select(" SELECT\n" +
            "        p.*\n" +
            "        FROM\n" +
            "        sys_user AS u\n" +
            "        LEFT JOIN sys_user_role_relation AS ur\n" +
            "        ON u.id = ur.user_id\n" +
            "        LEFT JOIN sys_role AS r\n" +
            "        ON r.id = ur.role_id\n" +
            "        LEFT JOIN sys_role_permission_relation AS rp\n" +
            "        ON r.id = rp.role_id\n" +
            "        LEFT JOIN sys_permission AS p\n" +
            "        ON p.id = rp.permission_id\n" +
            "        WHERE u.id = #{userId}\n")
    List<SysPermission> selectListByUser(Integer userId);

    /**
     * 查询具体某个接口的权限
     *
     * @param path 接口路径
     * @return
     */
    @Select("SELECT\n" +
            "            pe.*\n" +
            "        FROM\n" +
            "            sys_permission pe\n" +
            "        LEFT JOIN sys_request_path_permission_relation re ON re.permission_id = pe.id\n" +
            "        LEFT JOIN sys_request_path pa ON pa.id = re.url_id\n" +
            "        WHERE pa.url = #{path}")
    List<SysPermission> selectListByPath(String path);


}
